// CButtonAutoRepeat.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "CButtonAutoRepeat.h"
#include ".\cbuttonautorepeat.h"


#define INITIAL_DELAY 500
#define REPEAT_DELAY  100
#define IDT_TIMER 1

// CButtonAutoRepeat

IMPLEMENT_DYNAMIC(CButtonAutoRepeat, CButton)

BEGIN_MESSAGE_MAP(CButtonAutoRepeat, CButton)
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
    ON_WM_TIMER()
    ON_WM_LBUTTONDBLCLK()
    ON_WM_KEYDOWN()
    ON_WM_KEYUP()
END_MESSAGE_MAP()

//////////////////////////////////////////////////////////////////////////
CButtonAutoRepeat::CButtonAutoRepeat()
{
    mb_use_custom_color = false;
}

//////////////////////////////////////////////////////////////////////////
CButtonAutoRepeat::~CButtonAutoRepeat()
{
}

//////////////////////////////////////////////////////////////////////////
void CButtonAutoRepeat::OnLButtonDown(UINT nFlags, CPoint point)
{
    SetTimer(IDT_TIMER, INITIAL_DELAY, NULL);
    GetParent()->SendMessage(WM_COMMAND,
                             MAKELONG(GetDlgCtrlID(), BN_CLICKED),
                             (LPARAM)m_hWnd);
    CButton::OnLButtonDown(nFlags, point);
}

//////////////////////////////////////////////////////////////////////////
void CButtonAutoRepeat::OnLButtonUp(UINT , CPoint)
{
    KillTimer(IDT_TIMER);

    if (GetCapture() != NULL)
    {
        ReleaseCapture();
    }


    //CButton::OnLButtonUp(nFlags, point);
}

//////////////////////////////////////////////////////////////////////////
void CButtonAutoRepeat::OnTimer(UINT nIDEvent)
{
    if ((GetState() & BST_PUSHED) == 0)
        return;

    SetTimer(IDT_TIMER, REPEAT_DELAY, NULL);
    GetParent()->SendMessage(WM_COMMAND,
                             MAKELONG(GetDlgCtrlID(), BN_CLICKED),
                             (LPARAM)m_hWnd);
    sent++;

    CButton::OnTimer(nIDEvent);

}

//////////////////////////////////////////////////////////////////////////
void CButtonAutoRepeat::OnLButtonDblClk(UINT nFlags, CPoint point)
{
    if (nFlags & MK_LBUTTON)
        OnLButtonDown(nFlags, point);
    else
        GetParent()->SendMessage(WM_COMMAND,
                                 MAKELONG(GetDlgCtrlID(), BN_CLICKED),
                                 (LPARAM)m_hWnd);


    CButton::OnLButtonDblClk(nFlags, point);
}

//////////////////////////////////////////////////////////////////////////
void CButtonAutoRepeat::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{

    if (nChar==' ')
        GetParent()->SendMessage(WM_COMMAND,
                                 MAKELONG(GetDlgCtrlID(), BN_CLICKED),
                                 (LPARAM)m_hWnd);

    CButton::OnKeyDown(nChar, nRepCnt, nFlags);
}

//////////////////////////////////////////////////////////////////////////
void CButtonAutoRepeat::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    CButton::OnKeyUp(nChar, nRepCnt, nFlags);
}

//////////////////////////////////////////////////////////////////////////
void    CButtonAutoRepeat::SetBackground(COLORREF col)
{
    if (mb_use_custom_color && col == m_custom_color)   return;

    mb_use_custom_color = true;
    m_custom_color = col;
    Invalidate(FALSE);
}

/////////////////////////////////////////////////////////////////////////
void    CButtonAutoRepeat::SetDefaultBackground()
{
    if (!mb_use_custom_color)   return;

    mb_use_custom_color = false;
    Invalidate(FALSE);
}

//////////////////////////////////////////////////////////////////////////
void CButtonAutoRepeat::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    ASSERT(lpDrawItemStruct->itemID >= 0);

    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    CRect rect;

    // draw the colored rectangle portion
    rect.CopyRect(&lpDrawItemStruct->rcItem);


    UINT state = DFCS_BUTTONPUSH;
    if (lpDrawItemStruct->itemState & ODS_DISABLED)
    {
        state |= DFCS_INACTIVE;
        pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
    }
    else
        if (lpDrawItemStruct->itemState & ODS_SELECTED)
        {
            state |= DFCS_CHECKED;
        }


    if (mb_use_custom_color)
    {
        pDC->FillSolidRect(rect, m_custom_color);
        pDC->Draw3dRect(rect, GetSysColor(COLOR_BTNSHADOW), GetSysColor(COLOR_BTNSHADOW));
    }
    else
        pDC->DrawFrameControl(rect,DFC_BUTTON,state);


    // draw text
    int oldbkmode=pDC->SetBkMode(TRANSPARENT);
    rect.DeflateRect(1,1);
    rect -= CPoint(0,1);
    CString text;
    GetWindowText(text);
    pDC->DrawText(text,rect,DT_CENTER|DT_VCENTER|DT_NOCLIP|DT_SINGLELINE|DT_NOPREFIX);

    pDC->SetBkMode(oldbkmode);

};
